package com.yihu.wlyy.web.doctor.patient;

import com.yihu.wlyy.entity.patient.PatientDisease;
import com.yihu.wlyy.service.app.disease.PatientDiseaseService;
import com.yihu.wlyy.web.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jce.provider.symmetric.ARC4;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 患者疾病控制器
 *
 * Created by lyr on 2016/09/09.
 */
@RestController
@RequestMapping(value = "/doctor/patient")
public class PatientDiseaseController extends BaseController{

    @Autowired
    PatientDiseaseService patientDiseaseService;
    @Autowired
    StringRedisTemplate redisTemplate;

    /**
     * 查询患者的疾病
     *
     * @param patient 患者
     * @return
     */
    @RequestMapping(value = "/disease")
    public String getPatientDisease(String patient){
        try{
            if(StringUtils.isEmpty(patient)){
                return error(-1,"患者不能为空");
            }
            if(StringUtils.isEmpty(redisTemplate.opsForValue().get("disease:" + patient))) {
                List<PatientDisease> diseases = patientDiseaseService.getPatientDisease(patient);
                return write(200,"查询成功","data",new JSONArray(diseases));
            }else{
                return write(200,"查询成功","data",new JSONArray(redisTemplate.opsForValue().get("disease:" + patient)));
            }
        }catch (Exception e){
            e.printStackTrace();
            return error(-1,"查询失败");
        }
    }

    /**
     * 更新患者疾病
     *
     * @param patient 患者标识
     * @param disease 疾病
     * @return
     */
    @RequestMapping(value = "/disease_update")
    public String updatePatientDisease(String patient,String disease){
        try{
            if(patientDiseaseService.updatePatientDisease(patient,disease)){
                return write(200,"更新成功");
            }else{
                return error(-1,"更新失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return error(-1,"更新失败");
        }
    }

    /**
     * 更新到redis
     *
     * @return
     */
    @RequestMapping(value = "/to_redis")
    public String updateToRedid(){
        try{
            patientDiseaseService.updateToRedis();
            return write(200,"更新成功");
        }catch (Exception e){
            return error(-1,"更新失败");
        }
    }

    /**
     * 更新到疾病表
     *
     * @return
     */
    @RequestMapping(value = "/to_disease")
    public String updateToDisease(){
        try{
            patientDiseaseService.updateToDisease();
            return write(200,"更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return error(-1,"更新失败");
        }
    }

}
